package cn.itsource.lcw;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

public class DateUtil {
    public static SimpleDateFormat SDF_HH_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat SDF_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat SDF_HH_MM = new SimpleDateFormat("HH:mm");
    public static SimpleDateFormat SDF_HH_MM_SS = new SimpleDateFormat("HH:mm:ss");
    /**
     * 转换微信支付的付款时间格式
     */
    public static SimpleDateFormat SDF_WEIXIN_HHMMDDHHMMSS = new SimpleDateFormat("yyyyMMddHHmmss");
    public static SimpleDateFormat SDF_YYYYMMDD = new SimpleDateFormat("yyyyMMdd");

    /**
     * 获取当前的零辰零点时间
     *
     * @return
     */
    public static Date getNowOfZeroHourAndMinute() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        Date beginOfDate = calendar.getTime();

        return beginOfDate;
    }

    /**
     * 获取本周的零点时间
     *
     * @return
     */
    public static Date getCurrWeekOfZeroHourAndMinute() {
        Calendar cal = new GregorianCalendar();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date first = cal.getTime();
        return first;
    }

    /**
     * 获取本月的第1天
     *
     * @return
     */
    public static Date getCurrMonthOfZeroDayAndHour() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期即为本月第一天
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取num月前的第1天
     *
     * @param num
     * @return java.util.Date
     * @author caiwen
     * @date 2019-12-22
     */
    public static Date getNumMonthBeforeOfZeroDayAndHour(int num) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, num * (-1));
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期即为本月第一天
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取带年月日前缀的UUID随机数
     *
     * @return
     */
    public static String getDate4Year2Month2DateOfRandomStr() {
        String key = UUID.randomUUID().toString().replaceAll("-", "");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int date = calendar.get(Calendar.DAY_OF_MONTH);
        key = "" + year + (month < 10 ? ("0" + month) : month) + (date < 10 ? ("0" + date) : date) + key;
        return key;
    }

    public void test() {

//        System.out.println(sdf.format(getNowOfZeroHourAndMinute()));
//        System.out.println(sdf.format(getCurrWeekOfZeroHourAndMinute()));
//        System.out.println(sdf.format(getCurrMonthOfZeroDayAndHour()));
//        System.out.println(SDF_HH_MM_DD_HH_MM_SS.format(getNumMonthBeforeOfZeroDayAndHour(3)));

        System.out.println(SDF_HH_MM_DD_HH_MM_SS.format(getNowOfZeroHourAndMinute()));
//        try {
//            Date parse = SDF_HH_MM_DD.parse("2019-10-09");
//            System.out.println(parse);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
    }


}
